package com.iplant.wms.controller.batch;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceAPI;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.SMSSService;
import com.iplant.wms.service.po.batch.MSSBatchRule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/MSSBatchRule")
public class MSSBatchRuleController extends BasicController<MSSBatchRule> {

	public MSSBatchRuleController() {
		super(MSSBatchRule.class);
	}

	@Autowired
	SMSSService mSMSSService;

	/**
	 * 批量删除
	 */
	@TraceAPI("批量删除")
	@PostMapping("/DeleteList")
	public Object DeleteList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult;
		try {
			BMSEmployee wLoginUser = GetSession(request);


			if(!wParam.containsKey("data")){
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			List<MSSBatchRule> wMSSBatchRuleList = CloneTool.CloneArray(wParam.get("data"), mClazz);
			ServiceResult<Integer> wServiceResult =new ServiceResult<>(0);
			for (MSSBatchRule wMSSBatchRule : wMSSBatchRuleList) {

				wServiceResult = mSMSSService.MSS_DeleteBatchRule(wLoginUser, wMSSBatchRule);

				if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
					break;
			}

			if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@TraceAPI("删除")
	@PostMapping("/Delete")
	public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			if(!wParam.containsKey("data")){
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSBatchRule wMSSBatchRule = CloneTool.Clone(wParam.get("data"), mClazz);

			ServiceResult<Integer> wServiceResult = mSMSSService.MSS_DeleteBatchRule(wLoginUser, wMSSBatchRule);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}
}
